package com.cskaoyan.service;

import com.cskaoyan.bean.PO.*;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.mapper.MarketPermissionMapper;
import com.cskaoyan.mapper.MarketPermissionsThirdMapper;
import com.cskaoyan.mapper.MarketRoleMapper;
import com.cskaoyan.utils.MyGetCurrentTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName AdminAuthServiceImple
 * @Description
 * @Author Lxz
 * @Date 2022-8-16 9:56
 */
@Service
public class AdminAuthServiceImple implements AdminAuthService {
    @Autowired
    MarketAdminMapper adminMapper;
    @Autowired
    MarketRoleMapper roleMapper;
    @Autowired
    MarketPermissionMapper permissionMapper;
    @Autowired
    MarketPermissionsThirdMapper permissionsThirdMapper;


    @Override
    public MarketAdmin getAdminInfo(MarketAdmin primaryPrincipal) {
        MarketAdminExample example = new MarketAdminExample();
        example.createCriteria().andUsernameEqualTo(primaryPrincipal.getUsername());
        List<MarketAdmin> marketAdmins = adminMapper.selectByExample(example);
        MarketAdmin admin = marketAdmins.get(0);
        return admin;
    }

    @Override
    public ArrayList<String> getroles(MarketAdmin admin) {
        Integer[] roleIds = admin.getRoleIds();
        ArrayList<String> rolesName = new ArrayList<>();
        MarketRoleExample example = new MarketRoleExample();
        for (int i = 0; i < roleIds.length; i++) {
            example.createCriteria().andIdEqualTo(roleIds[i]);
            List<MarketRole> marketRoles = roleMapper.selectByExample(example);
            rolesName.add(marketRoles.get(0).getName());
        }
        return rolesName;
    }

    @Override
    public ArrayList<String> getPerms(MarketAdmin admin) {
        Integer[] roleIds = admin.getRoleIds();
        ArrayList<String> perms = new ArrayList<>();
        MarketPermissionExample example = new MarketPermissionExample();
        for (int i = 0; i < roleIds.length; i++) {
            example.createCriteria().andRoleIdEqualTo(roleIds[i]);
            List<MarketPermission> marketPermissionList = permissionMapper.selectByExample(example);
            if ("*".equals(marketPermissionList.get(0).getPermission())) {
                perms.add("*");
                return perms;
            }
            for (MarketPermission marketPermission : marketPermissionList) {
                MarketPermissionsThirdExample thirdExample = new MarketPermissionsThirdExample();
                thirdExample.createCriteria().andNameidEqualTo(marketPermission.getPermission());
                List<MarketPermissionsThird> marketPermissionsThirdList = permissionsThirdMapper.selectByExample(thirdExample);
                for (MarketPermissionsThird marketPermissionsThird : marketPermissionsThirdList) {
                    perms.add(marketPermissionsThird.getApi());
                }
            }
        }
        return perms;
    }

    @Override
    public void updateData(MarketAdmin primaryPrincipal) {
        Date currentTime = MyGetCurrentTime.getCurrentTime();
        primaryPrincipal.setLastLoginTime(currentTime);
        primaryPrincipal.setUpdateTime(currentTime);
        MarketAdminExample example = new MarketAdminExample();
        example.createCriteria().andUsernameEqualTo(primaryPrincipal.getUsername());
        adminMapper.updateByExampleSelective(primaryPrincipal, example);
    }


}
